package com.fingard.dsp.bank.directbank.mbs01;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.directbank.cmbunion02.utils.SignatureUtil;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.text.StringHelper;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class MBS01OtherElse extends MBS01Base implements IOtherElse {
    @Override
    public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
        RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
        try{
            String transType = p_reqElse.reqHeader.transType;
            if ("VerifySign".equals(transType)) {
                String ownAct = "";
                if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
                    ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
                }
                String plain = "";
                if (p_reqElse.reqHashMap.containsKey("Plain")) {
                    plain = URLDecoder.decode(p_reqElse.reqHashMap.get("Plain"), "utf-8");
                } else {
                    retAts.retHashMap.put("RespCode", "-1");
                    retAts.retHashMap.put("RespInfo", "缺失验签原始串");

                    return retAts;
                }
                WriteBankLogLn("麦保付通知报文："+plain);
                ActSetItem actItem = this.getBankConfig().getActSetItem(ownAct, "");
                if (actItem != null) {
                    JSONObject resultObj = JSONObject.parseObject(plain);
                    String sign = resultObj.remove("sign").toString();
                    String digest = getSignData(resultObj,actItem.saltKey);
                    WriteBankLogLn("生成签名串："+digest);
                    boolean result = sign.equalsIgnoreCase(digest);
                    retAts.retHashMap = new HashMap<String, String>();
                    if (result) {
                        retAts.retHashMap.put("RespCode", "0");
                        retAts.retHashMap.put("RespInfo", "验签通过");
                    } else {
                        retAts.retHashMap.put("RespCode", "-1");
                        retAts.retHashMap.put("RespInfo", "验签失败");
                    }
                } else {
                    retAts.retHashMap.put("RespCode", "-1");
                    retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
                }
            }else{
                retAts.retHashMap = new HashMap<String, String>();
                retAts.retHashMap.put("RespCode", "-1");
                retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);


            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.retHashMap.put("RespCode", "-1");
            retAts.retHashMap.put("RespInfo", "1111"+ex.getMessage());
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
